#! python3
# -*- encoding: utf-8 -*-
'''
@File    :   CAR.py
@Time    :   2020/10/20 16:14:07
@Author  :   lyc
@Version :   1.0
@Contact :   lyc0209@qq.com
'''
import RPi.GPIO as GPIO         # 引入GPIO模块
import time                     # 引入time模块
from car_config import *

# 四个轮子依次以x形安装， 则
# 前进：  四个轮子均向前
# 后退：  四个轮子均向后
# 左平移：左前、右后轮向后，右前、左后轮向前
# 右平移：左前、右后轮向前，右前、左后轮向后

class CAR:
    def __init__(self):
        # 定义四个轮子的引脚
        self.wheel_left_front = wheel_left_front
        self.wheel_right_front = wheel_right_font
        self.wheel_left_rear = wheel_left_rear
        self.wheel_right_rear = wheel_right_rear

        GPIO.setmode(GPIO.BCM)

        for i in range(3):
            GPIO.setup(self.wheel_left_front[i], GPIO.OUT)
            GPIO.setup(self.wheel_right_front[i], GPIO.OUT)
            GPIO.setup(self.wheel_left_rear[i], GPIO.OUT)
            GPIO.setup(self.wheel_right_rear[i], GPIO.OUT)

    def run(self, motor, direction):
        # motor: 控制哪一个轮子 0 左前轮 1 右前轮 2 左后轮 3 右后轮
        # direction: 方向， 0 -> 前进， 1 -> 后退
        if motor == 0:
            if direction == 0:
                GPIO.output(self.wheel_left_front[1], False)
                GPIO.output(self.wheel_left_front[2], True)
                GPIO.output(self.wheel_left_front[0], True)
            else:
                GPIO.output(self.wheel_left_front[1], True)
                GPIO.output(self.wheel_left_front[2], False)
                GPIO.output(self.wheel_left_front[0], True)
        elif motor == 1:
            if direction == 0:
                GPIO.output(self.wheel_right_front[1], False)
                GPIO.output(self.wheel_right_front[2], True)
                GPIO.output(self.wheel_right_front[0], True)
            else:
                GPIO.output(self.wheel_right_front[1], True)
                GPIO.output(self.wheel_right_front[2], False)
                GPIO.output(self.wheel_right_front[0], True) 
        elif motor == 2:
            if direction == 0:
                GPIO.output(self.wheel_left_rear[1], False)
                GPIO.output(self.wheel_left_rear[2], True)
                GPIO.output(self.wheel_left_rear[0], True)
            else:
                GPIO.output(self.wheel_left_rear[1], True)
                GPIO.output(self.wheel_left_rear[2], False)
                GPIO.output(self.wheel_left_rear[0], True)                    
        elif motor == 3:
            if direction == 0:
                GPIO.output(self.wheel_right_rear[1], False)
                GPIO.output(self.wheel_right_rear[2], True)
                GPIO.output(self.wheel_right_rear[0], True)
            else:
                GPIO.output(self.wheel_right_rear[1], True)
                GPIO.output(self.wheel_right_rear[2], False)
                GPIO.output(self.wheel_right_rear[0], True) 

    def stop(self, motor):
        if motor == 0:
            GPIO.output(self.wheel_left_front[0], False)            
            GPIO.output(self.wheel_left_front[1], False)
            GPIO.output(self.wheel_left_front[2], False)
        elif motor == 1:
            GPIO.output(self.wheel_right_front[0], False)            
            GPIO.output(self.wheel_right_front[1], False)
            GPIO.output(self.wheel_right_front[2], False)
        elif motor == 2:
            GPIO.output(self.wheel_left_rear[0], False)            
            GPIO.output(self.wheel_left_rear[1], False)
            GPIO.output(self.wheel_left_rear[2], False)
        elif motor == 3:
            GPIO.output(self.wheel_right_rear[0], False)            
            GPIO.output(self.wheel_right_rear[1], False)
            GPIO.output(self.wheel_right_rear[2], False)            
                        


    # 前进
    def go_forward(self):
        for i in range(4):
            self.run(i, 0)
    # 后退
    def go_back(self):
        for i in range(4):
            self.run(i, 1)
    # 停止
    def go_stop(self):
        for i in range(4):
            self.stop(i)
    # 左平移
    def go_left(self):
        self.run(0, 1)
        self.run(3, 1)
        self.run(1, 0)
        self.run(2, 0)
    #右平移
    def go_right(self):
        self.run(0, 0)
        self.run(3, 0)
        self.run(1, 1)
        self.run(2, 1)


        


